﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;


namespace YK.Framework.Container
{
    public class ClassicContainer
    {
        private static Spring.Context.Support.StaticApplicationContext context;

        static ClassicContainer()
        {
            context = new Spring.Context.Support.StaticApplicationContext();
        }     

        public static void Register<T>()
        {
            Register<T>(false);
        }

        public static void Register<T>(bool isSingleton)
        {
            if (!isSingleton)
            {
                context.RegisterPrototype(typeof(T).FullName, typeof(T), new Spring.Objects.MutablePropertyValues());
            }
            else
            {
                context.RegisterSingleton(typeof(T).FullName, typeof(T), new Spring.Objects.MutablePropertyValues());
            }
        }      

        public static void Register<T>(string name)
        {
            Register<T>(name, false);
        }

        public static void Register<T>(string name, bool isSingleton)
        {
            if (!isSingleton)
            {
                
                context.RegisterPrototype(name, typeof(T), new Spring.Objects.MutablePropertyValues());
            }
            else
            {
                context.RegisterSingleton(name, typeof(T), new Spring.Objects.MutablePropertyValues());
            }
        }

        public static void Register<T>(string name, IDictionary properties)
        {
            Register<T>(name, properties, false);
        }

        public static void Register<T>(string name, IDictionary properties, bool isSingleton)
        {
            if (!isSingleton)
            {
                context.RegisterPrototype(name, typeof(T), new Spring.Objects.MutablePropertyValues(properties));
            }
            else
            {
                context.RegisterSingleton(name, typeof(T), new Spring.Objects.MutablePropertyValues(properties));
            }
        }       

        public static object GetObject(string name)
        {
            if (context.ContainsObject(name))
            {
                return context.GetObject(name);
            }

            return null;
        }

        public static object GetObject<T>()
        {
            var objects = context.GetObjectsOfType(typeof(T));

            if (objects.Count > 0)
            {
                return objects.Cast<DictionaryEntry>().First().Value;
            }

            return null;
        }

        public static object GetObject(Type type)
        {
            var objects = context.GetObjectsOfType(type);

            if (objects.Count > 0)
            {
                return objects.Cast<DictionaryEntry>().First().Value;
            }

            return null;
        }

        public static IDictionary GetObjects<T>()
        {
            return context.GetObjectsOfType(typeof(T));
        }

        public static IDictionary GetObjects(Type type)
        {
            return context.GetObjectsOfType(type);
        }

        public static object ConfigureObject(object target, string name)
        {      
            return context.ConfigureObject(target, name);
        }
    }
}